<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class gamescore extends CI_Controller {

	function __construct() {
        parent::__construct();
		$this->load->model("mgamescore");
		$this->load->model("mgamescores");
		$this->load->model("muser");
		$this->load->model("mgame");
    }
	function index()
	{
	
	}
	public function get_token($game_key = ''){
		$token =  md5(uniqid(rand(), true));
		$game_session = array();
		if(!$this->muser->checkLogin())
		{
			//$game_session = $this->session->userdata('game');
			die();
		}

		$game_session = $this->session->userdata('game');
		if(!$game_session)
			$game_session = array();
		if(!isset($game_session[$game_key])){
			//$game_session[] = $game_key;
		}
		$game_session[$game_key] = $token;
		$this->session->set_userdata(array('game'=>$game_session));
		die($token);
	}
	
	public function newscore(){
		$score = intval($this->input->post("gscore"));
		$key = $this->input->post("gkey");
		$token =$this->input->post("gtoken");
		if(!$score || !$key || !$token)
			die();
		
		$game_session = $this->session->userdata('game');
		if(!$this->muser->checkLogin() || $key=='' || !isset($game_session[$key])|| $game_session[$key] !=$token)
		{
			die();
		}
		$game = $this->mgame->get($key, 'game_key');
		if(!$game) die();
		
		//insert diem to log
		$arr_insert= array();
		$arr_insert['game_id'] = $game->game_id;
		$arr_insert['use_id'] = $this->muser->use_id;
		$arr_insert['gscore_scores'] = $score;
		$arr_insert['gscore_time_create'] = time();
		$arr_insert['gscore_time_update'] = time();
		$arr_insert['gscore_ip'] =  $this->input->ip_address();
		$this->mgamescores->insert($arr_insert);
		
		//update diem
		$check = $this->mgamescore->get(array(	'game_id'=>$game->game_id,
												'use_id'=>$this->muser->use_id));
		if(!$check){
			$arr_insert['gscore_score'] = $score;
			$this->mgamescore->insert($arr_insert);
		}
		else {
			$arr_update= array();
			$arr_update['gscore_score'] = $score;
			$arr_update['gscore_time_update'] = time();
			$arr_update['gscore_ip'] = $this->input->ip_address();
			$this->db->where('use_id',$this->muser->use_id);
			$this->mgamescore->update($game->game_id, 'game_id',$arr_update);
		}
	}
	function tf(){
		//SWF
		$time_start = time();
		require(APPPATH.'libraries/SWF.php');
		//$filename = './dataupload/game/data/Game_BanGa.swf';
		//$filename = './dataupload/game/data/Game_BuoiDaHoiBatNgo.swf';
		$filename = './dataupload/game/data/Game_TomVaJerryDuaXeDap.swf';
		$filename = './dataupload/game/data/Game_FlappyBird.swf';
		//$x = getimagesize('./dataupload/game/data/Game_BanGa.swf');
		 $s = new SWF(file_get_contents($filename));
	//	var myLoader:URLLoader = new URLLoader();
	$flag = false;
	$gname = '';
		 foreach ($s->tags as $tag) {
            $ret = $s->parseTag($tag);
		print_r($ret);
			continue;
           if(isset($ret['actions'])){
				if(isset($ret['actions'][0])){
					if(isset($ret['actions'][0]['actionData'])){
						$datas = $ret['actions'][0]['actionData'];
						foreach($datas as $k =>$v){
							if($flag == true){
								$gname = $v;
								print_r('GName: '.$gname);
								die();
							}
							if($v =='gname'){
								$flag = true;
							}
							
						}
						 
					}
			   
			   }
		   
		   }
        }
		$time_end = time();
		$total = $time_end - $time_start;
		echo "time: ".$total;
		print_r('GName: '.$gname);
	}
}